import {isArr} from "@/utils/arrayUtil";
import {authentication} from "@/jurisdiction/authentication";
import {LOGICAL} from "@/constantData";

/**
 * 操作类型编码
 */
const HANDEL_CODE_CONFIG = {
  // 追加检查目标
  ADD_TARGET: '1',
  // 任务完成情况
  PERFORMANCE: '2',
  // 业务数据统计
  DATA_STATISTICS: '3',
  // 填报隐患
  ADD_HIDDEN_DANGER: 'qy_sbyh',
  // 分管领导确认 -- 复核数据
  REVIEW_DATA_FGLDQR: 'qy_ldqr',
  // 盟市初审 -- 复核数据
  REVIEW_DATA_MSCS: 'ms_cs',
  // 盟市领导确认 -- 复核数据
  REVIEW_DATA_MSLDQR: 'ms_ldqr',
  // 旗县初审 -- 复核数据
  REVIEW_DATA_QXCS: 'qx_cs',
  // 旗县领导确认 -- 复核数据
  REVIEW_DATA_QXLDQR: 'qx_ldqr',
  // 自治区初审 -- 复核数据
  REVIEW_DATA_ZZQCS: 'zzq_cs',
  // 自治区领导确认 -- 复核数据
  REVIEW_DATA_ZZQLDQR: 'zzq_ldqr',
  // 接收
  RECEPTION: '6',
  // 启动
  START: 'start'
}

/**
 * 追加检查目标
 * @type {{icon: string, label: string, typeCode: string}}
 */
const ADD_TARGET = {
  handelCode: HANDEL_CODE_CONFIG.ADD_TARGET,
  icon: 'el-icon-s-tools',
  label: '追加检查目标',
  funName: 'addTo',
  param: {},
  authority: {role: ['A:B:A'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  //   {
  //     key: 'level',
  //     value: [3],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 任务完成情况
 * @type {{icon: string, label: string, typeCode: string}}
 */
const PERFORMANCE = {
  handelCode: HANDEL_CODE_CONFIG.PERFORMANCE,
  icon: 'el-icon-s-tools',
  label: '任务完成情况',
  funName: '',
  param: {},
  authority: {role: ['A:B:B'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   }
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 业务数据统计
 * @type {{icon: string, label: string, handelCode: string}}
 */
const DATA_STATISTICS = {
  handelCode: HANDEL_CODE_CONFIG.DATA_STATISTICS,
  icon: 'el-icon-s-tools',
  label: '业务数据统计',
  funName: '',
  param: {},
  authority: {role: ['A:B:C'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   }
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 填报隐患
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const ADD_HIDDEN_DANGER = {
  handelCode: HANDEL_CODE_CONFIG.ADD_HIDDEN_DANGER,
  icon: 'el-icon-s-tools',
  label: '填报隐患',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:D'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   }
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 分管领导确认 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_FGLDQR = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_FGLDQR,
  icon: 'el-icon-s-tools',
  label: '复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'acceptStateType',
      value: [2],
      logical: LOGICAL.OR,
    },
    {
      key: 'isRoot',
      value: [1],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 盟市初审 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_MSCS = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_MSCS,
  icon: 'el-icon-s-tools',
  label: '盟市初审-复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 盟市领导确认 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_MSLDQR = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_MSLDQR,
  icon: 'el-icon-s-tools',
  label: '盟市领导确认-复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 旗县初审 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_QXCS = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_QXCS,
  icon: 'el-icon-s-tools',
  label: '旗县初审-复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 旗县领导确认 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_QXLDQR = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_QXLDQR,
  icon: 'el-icon-s-tools',
  label: '旗县领导确认-复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 自治区初审 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_ZZQCS = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_ZZQCS,
  icon: 'el-icon-s-tools',
  label: '自治区初审-复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 自治区领导确认 -- 复核数据
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const REVIEW_DATA_ZZQLDQR = {
  handelCode: HANDEL_CODE_CONFIG.REVIEW_DATA_ZZQLDQR,
  icon: 'el-icon-s-tools',
  label: '自治区领导确认-复核数据',
  funName: 'goPage',
  param: {path: '/securityCheck/defectQuestion'},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'level',
  //     value: [1, 2],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.OR,
}

/**
 * 接收
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const RECEPTION = {
  handelCode: HANDEL_CODE_CONFIG.RECEPTION,
  icon: 'el-icon-s-tools',
  label: '接收',
  funName: 'reception',
  param: {},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'acceptStateType',
      value: [1, 3, 4],
      logical: LOGICAL.OR,
    },
    {
      key: 'isRoot',
      value: [1],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 启动
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const START = {
  handelCode: HANDEL_CODE_CONFIG.START,
  icon: 'el-icon-s-tools',
  label: '启动',
  funName: 'startExamine',
  param: {},
  authority: {role: ['A:B:E'], logical: LOGICAL.AND},
  // dataFilter: [
  //   {
  //     key: 'acceptStateType',
  //     value: [1, 3, 4],
  //     logical: LOGICAL.OR,
  //   },
  //   {
  //     key: 'isRoot',
  //     value: [1],
  //     logical: LOGICAL.OR,
  //   },
  // ],
  // dataFilterLogical: LOGICAL.AND,
}


const EVENT_CONFIG = {
  //  自治区
  'zxjc_zzq': [START, REVIEW_DATA_ZZQCS, REVIEW_DATA_ZZQLDQR],
  //  盟市
  'zxjc_ms': [REVIEW_DATA_MSCS,REVIEW_DATA_MSLDQR,START, RECEPTION, ADD_TARGET, PERFORMANCE, DATA_STATISTICS, ADD_HIDDEN_DANGER,],
  //  旗县
  'zxjc_qx': [START, RECEPTION, ADD_TARGET, PERFORMANCE, DATA_STATISTICS, ADD_HIDDEN_DANGER,],
  //  企业
  'zxjc_qy': [START, RECEPTION, ADD_HIDDEN_DANGER, REVIEW_DATA_FGLDQR,],
}

const getEventBtList = (nodeInfo, data) => {
  let cBtItem = EVENT_CONFIG[data.nodeType];
  if (!isArr(cBtItem)) return [];
  cBtItem = _.cloneDeep(cBtItem).filter(item => {
    return authentication(item.authority)
  })
  if (!isArr(cBtItem)) return [];
  return _.cloneDeep(cBtItem).filter(item => {
    let {dataFilter, dataFilterLogical} = item
    if (!isArr(dataFilter) && (!dataFilterLogical || dataFilterLogical == '')) return true
    let dataFlagList = dataFilter.map(fItem => {
      let {key, value, logical} = fItem
      let vaFlagList = value.map(v => v == data[key])
      return logical == LOGICAL.OR ? vaFlagList.some(flag => flag) : vaFlagList.every(flag => flag)
    });
    return dataFilterLogical == LOGICAL.OR ? dataFlagList.some(flag => flag) : dataFlagList.every(flag => flag)
  })
}

export {getEventBtList, EVENT_CONFIG, HANDEL_CODE_CONFIG}
